<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="83px">
      <el-form-item label="用户账号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户账号"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属子系统" prop="sysCode">
        <el-select v-model="queryParams.sysCode" placeholder="请选择所属子系统" style="width: 240px" clearable>
          <el-option
            v-for="dict in dict.type.sys_code"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="请求接口" prop="bizType">
        <el-input
          v-model="queryParams.bizType"
          placeholder="请输入接口名称"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="TraceId" prop="traceId">
        <el-input
          v-model="queryParams.traceId"
          placeholder="请输入请求TraceId"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请求时间">
        <el-date-picker
          v-model="daterangeStartTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="logList" height="330px">
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ (pageParams.current-1)*pageParams.size + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="请求接口" align="center" prop="bizType" />
      <el-table-column label="用户账号" align="center" prop="userId" />
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column label="TraceId" align="center" prop="traceId" min-width="120" />
      <el-table-column label="请求结果代码" align="center" prop="code" />
      <el-table-column label="所属子系统" align="center" prop="sysCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_code" :value="scope.row.sysCode"/>
        </template>
      </el-table-column>
      <el-table-column label="请求时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.startTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="请求耗时" align="center" prop="clhs" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['system:log:list']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pageParams.current"
      :limit.sync="pageParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改系统日志对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户账号：">{{ form.userId }} </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户名：">{{ form.userName }} </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属子系统：">
              <template>
                <dict-tag :options="dict.type.sys_code" :value="form.sysCode"/>
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求url：">{{ form.url }} </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="接口方法：">{{ form.className }}{{ '.' }}{{ form.methodName }} </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="TraceId：">{{ form.traceId }} </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开始时间：">{{ form.startTime }} </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间：">{{ form.endTime }} </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求结果：">{{ form.code }} </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求耗时：">{{ form.clhs }}{{ 'ms' }} </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="请求参数：">{{ form.requestArgs }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="响应结果：">{{ form.operResult }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="service记录：" v-if="form.operDetail !== undefined && form.operDetail !== null && form.operDetail.length > 0">{{ form.operDetail }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="异常信息：" v-if="form.msg !== undefined && form.msg !== null && form.msg.length > 0">{{ form.msg }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLog } from "@/api/system/log";

export default {
  name: "Log",
  dicts: ['sys_code'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 系统日志表格数据
      logList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // service层记录信息时间范围
      daterangeStartTime: [],
      // 分页参数
      pageParams: {
        current: 1,
        size: 10,
      },
      // 查询参数
      queryParams: {
        id: null,
        userId: null,
        userName: null,
        traceId: null,
        sysCode: null,
        startTime: null,
        beginStartTime: null,
        endStartTime: null,
        bizType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {},
  beforeRouteEnter(to, from, next) {
    next(vm => {
      vm.handleQuery()
    })
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParams.current = 1;
      this.getList();
    },
    /** 查询系统日志列表 */
    getList() {
      this.loading = true;
      if (null != this.daterangeStartTime && '' != this.daterangeStartTime) {
        this.queryParams["beginStartTime"] = this.daterangeStartTime[0];
        this.queryParams["endStartTime"] = this.daterangeStartTime[1];
      } else {
        this.queryParams["beginStartTime"] = null;
        this.queryParams["endStartTime"] = null;
      }
      listLog(this.pageParams,this.queryParams).then(response => {
        this.logList = response.page.records;
        this.total = response.page.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bizType: null,
        userId: null,
        userName: null,
        traceId: null,
        className: null,
        methodName: null,
        code: null,
        msg: null,
        sysCode: null,
        url: null,
        startTime: null,
        endTime: null,
        clhs: null,
        requestArgs: null,
        operResult: null,
        operDetail: null
      };
      this.resetForm("form");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeStartTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查看详情操作 */
    handleView(row) {
      this.title = "查看详情";
      this.open = true;
      this.form = row;
    }
  }
};
</script>
